﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Collections;

namespace Web.Function
{
    /// <summary>
    /// 缓存管理类
    /// </summary>
    public static class CacheManager
    {
        /// <summary>
        /// 获取缓存
        /// </summary>
        public static T GetCache<T>(string name)
        {
            return (T)HttpContext.Current.Cache.Get(name);
        }

        /// <summary>
        /// 设置缓存
        /// </summary>
        public static T SetCache<T>(string name, T value)
        {
            if (HttpContext.Current.Cache[name] != null)
                HttpContext.Current.Cache[name] = value;
            else
                HttpContext.Current.Cache.Add(name, value, null, DateTime.MaxValue, new TimeSpan(1, 0, 0), CacheItemPriority.Default, null);
            return value;
        }

        /// <summary>
        /// 缓存判断
        /// </summary>
        public static bool Contains(string name)
        {
            return (HttpContext.Current.Cache[name] != null);
        }

        /// <summary>
        /// 清空缓存
        /// </summary>
        public static void Clear()
        {
            foreach (DictionaryEntry ch in HttpContext.Current.Cache)
                HttpContext.Current.Cache.Remove(ch.Key.ToString());
        }
    }
}
